package com.zhihuishu.micro.course.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.github.abel533.mapper.Mapper;
import com.zhihuishu.micro.course.dao.LessonMapper;
import com.zhihuishu.micro.course.dao.LessonVideoMapper;
import com.zhihuishu.micro.course.dao.TipInfoMapper;
import com.zhihuishu.micro.course.helper.RedisKeyHelper;
import com.zhihuishu.micro.course.model.TipInfo;
import com.zhihuishu.micro.course.service.TipInfoService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper.Handler;

/**
 * 
 * @author 嵇鹏
 * @descption:
 * 
 * 
 */
@Service("tipInfoService")
public class TipInfoServiceImpl extends BaseServiceImpl<TipInfo> implements TipInfoService {

    @Resource
    private TipInfoMapper tipInfoMapper;
    @Resource
    private LessonVideoMapper lessonVideoMapper;
    @Resource
    private LessonMapper lessonMapper;


    @Override
    public Mapper<TipInfo> getMapper() {
        
        return tipInfoMapper;
    }

    @Override
    public void saveOrUpdateTipInfo(final TipInfo tipInfo) {
        Integer id = tipInfoMapper.queryIdByUniqueKey(tipInfo);
        if (logger.isDebugEnabled()) {
            logger.debug("查询到的主键id为{}", id);

        }
        if (id == null) {// 没有记录
            saveTipInfo(tipInfo);
        } else {
            updateTipInfo(tipInfo, id);
        }

    }
     //更新修改记录
    private void updateTipInfo(final TipInfo tipInfo, Integer id) {
        // 有记录(更新)
        tipInfo.setId(id);
        tipInfoMapper.updateByPrimaryKey(tipInfo);
        final Map<String, String> map = BeanHelper.describe(tipInfo);
        jedisExecutor.execute( new Handler() {

            @Override
            public void handle(Jedis jedis) {
                jedis.del(JedisHelper.key("cc:tip", tipInfo.getId(), "info"));// 删除原来缓存
                jedis.hmset(JedisHelper.key("cc:tip", tipInfo.getId(), "info"), map);// 对象添加到缓存中

            }
        });
    }
    //保存修改记录
    private void saveTipInfo(final TipInfo tipInfo) {
        tipInfoMapper.insert(tipInfo);
        if (logger.isDebugEnabled()) {
            logger.debug("保存的id为:{}",tipInfo.getId());
        }
        final Map<String, String> map = BeanHelper.describe(tipInfo);
        jedisExecutor.execute( new Handler() {
            @Override
            public void handle(Jedis jedis) {
                ;
                jedis.hmset(RedisKeyHelper.getTipInfoKey(tipInfo.getId()), map);// 对象添加到缓存中
                jedis.sadd(RedisKeyHelper.getCourseTipSetKey(tipInfo.getCourseId()), tipInfo.getId().toString());// 添加到队列中

            }
        });
    }

    @Override
    public void deleteTipInfo(final TipInfo tipInfo) {
        final Integer id = tipInfoMapper.queryIdByUniqueKey(tipInfo);
        if (logger.isDebugEnabled()) {
            logger.debug("查询到的主键为{}",id);
        }
        if (id==null) {
            return;
        }
        tipInfoMapper.deleteTipInfo(tipInfo);
        jedisExecutor.execute( new Handler() {
            
            @Override
            public void handle(Jedis jedis) {
                jedis.srem(RedisKeyHelper.getCourseTipSetKey(tipInfo.getCourseId()),id.toString());//从列表中删除'
               
                jedis.del(RedisKeyHelper.getTipInfoKey(id));// 删除原来缓存

            }
        });

    }

    @Override
    public void deleteTipInfoByLessonId(Integer lessonId,Long courseId) {
        
        List<Integer> lessonVideoIds=lessonVideoMapper.queryLessonVideoIdsByLessonId(lessonId);
        if (logger.isDebugEnabled()) {
            logger.debug("查询到节下小节的id为{}",lessonVideoIds);
        }
        TipInfo tipInfo=null;
        tipInfo=new TipInfo();
        tipInfo.setCourseId(courseId);
        tipInfo.setModule(3);
        tipInfo.setChangePoint(6);//节视频
        tipInfo.setPointId(lessonId);
        deleteTipInfo(tipInfo);//删除节提醒
        if (lessonVideoIds!=null&&lessonVideoIds.size()>0) {
           
            for (Integer integer : lessonVideoIds) {//删除小节提醒
                tipInfo=new TipInfo();
                tipInfo.setCourseId(courseId);
                tipInfo.setModule(3);
                tipInfo.setChangePoint(7);//子节视频
                tipInfo.setPointId(integer);
                deleteTipInfo(tipInfo); 
            }
        }
        
    }

    @Override
    public void deleteTipInfoByChapterId(Integer chapterId, Long courseId) {
       List<Integer> lessonIds = lessonMapper.queryLessonIdsByChapterId(chapterId);
       if (logger.isDebugEnabled()) {
        logger.debug("查询到节ids为:{}",lessonIds);
      }
      if (lessonIds!=null&&lessonIds.size()>0) {
          TipInfo info=null;
        for (Integer integer : lessonIds) {
            info=new TipInfo();
            info.setCourseId(courseId);
            info.setModule(3);
            info.setChangePoint(6);//节视频
            info.setPointId(integer);
            deleteTipInfo(info);
        }
        
        List<Integer> lessonVideoIds=lessonVideoMapper.queryLessonVideoIdsByChapterId(chapterId);
        if (logger.isDebugEnabled()) {
            logger.debug("查询到节下小节的id为{}",lessonVideoIds);
        }
        if (lessonVideoIds!=null&&lessonVideoIds.size()>0) {
            TipInfo tipInfo=null;
            for (Integer integer : lessonVideoIds) {
                tipInfo=new TipInfo();
                tipInfo.setCourseId(courseId);
                tipInfo.setModule(3);
                tipInfo.setChangePoint(7);//子节视频
                tipInfo.setPointId(integer);
                deleteTipInfo(tipInfo); 
            }
        }
        
    }
        
    }

    @Override
    public void deletetTipInfoByLessonVideoId(int lessonVideoId, long courseId) {
        TipInfo tipInfo=new TipInfo();
        tipInfo.setCourseId(courseId);
        tipInfo.setModule(3);
        tipInfo.setChangePoint(7);
        tipInfo.setPointId(lessonVideoId);
        deleteTipInfo(tipInfo);
        
    }

}
